package com.qcby.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.community.entity.Community;
import com.qcby.community.entity.InOutRecord;
import com.qcby.community.entity.Person;
import com.qcby.community.form.InOutQueryForm;
import com.qcby.community.mapper.CommunityMapper;
import com.qcby.community.mapper.InOutRecordMapper;
import com.qcby.community.mapper.PersonMapper;
import com.qcby.community.service.InOutRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.community.vo.ChartVO;
import com.qcby.community.vo.InOutRecordVO;
import com.qcby.community.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-03-25
 */
@Service
public class InOutRecordServiceImpl extends ServiceImpl<InOutRecordMapper, InOutRecord> implements InOutRecordService {
    @Autowired
    private InOutRecordMapper inOutRecordMapper;

    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private PersonMapper personMapper;

    @Override
    public Map chart() {
        //从数据库中获取所有的小区名称以及小区所对应的人数
        List<ChartVO> chartVOList = this.inOutRecordMapper.chart();
        //用来装所有小区名称的集合
        List<String> names = new ArrayList<>();
        //用来装所有小区对应的人数的集合
        List<Integer> nums = new ArrayList<>();
        //list和chartVOList区别在于一个用于柱状图格式 一个用于饼图格式
        List<ChartVO> list = new ArrayList<>();
        //遍历所有的小区以及小区对应的人数
        for (ChartVO chartVO : chartVOList) {
            names.add(chartVO.getName());
            nums.add(chartVO.getValue());
            list.add(chartVO);
        }
        Map<String,List> map = new HashMap<>();
        map.put("names",names);
        map.put("nums",nums);
        map.put("list",list);
        return map;
    }

    /**
     * 分页条件查询所有的出入记录
     * @param inOutQueryForm
     * @return
     */
    @Override
    public PageVO inoutList(InOutQueryForm inOutQueryForm) {
        //按照原有的MP提供的方式查询所有的分页信息
        Page<InOutRecord> page = new Page<>(inOutQueryForm.getPage(),inOutQueryForm.getLimit());
        //构造查询条件，查询所有符合要求的居民信息
        QueryWrapper<Person> personQueryWrapper = new QueryWrapper<>();
        personQueryWrapper.like(StringUtils.isNotBlank(inOutQueryForm.getUserName()),"username",inOutQueryForm.getUserName());
        List<Person> personList = new ArrayList<>();
        //创建包括所有居民id的集合
        List<Integer> idList = new ArrayList<>();
        //遍历所有居民，获取id，装入idlist集合
        for (Person person : personList) {
            idList.add(person.getPersonId());
        }
        QueryWrapper<InOutRecord> inOutRecordQueryWrapper = new QueryWrapper<>();
        inOutRecordQueryWrapper.in(idList.size()>0,"person_id",idList);
        inOutRecordQueryWrapper.between(StringUtils.isNotBlank(inOutQueryForm.getStartDate()) && StringUtils.isNotBlank(inOutQueryForm.getEndDate()),"in_time",inOutQueryForm.getStartDate(),inOutQueryForm.getEndDate());
        Page<InOutRecord> resultPage = inOutRecordMapper.selectPage(page,inOutRecordQueryWrapper);
        PageVO pageVO = new PageVO();
        List<InOutRecordVO> list = new ArrayList<>();
        for (InOutRecord record : resultPage.getRecords()) {
            InOutRecordVO inOutRecordVO = new InOutRecordVO();
            BeanUtils.copyProperties(record,inOutRecordVO);
            Community community = this.communityMapper.selectById(record.getCommunityId());
            inOutRecordVO.setCommunityName(community.getCommunityName());
            Person person = this.personMapper.selectById(record.getPersonId());
            inOutRecordVO.setTermName(person.getTermName());
            inOutRecordVO.setHouseNo(person.getHouseNo());
            inOutRecordVO.setUserName(person.getUserName());
            list.add(inOutRecordVO);
        }
        pageVO.setList(list);
        pageVO.setTotalCount(resultPage.getTotal());
        pageVO.setPageSize(resultPage.getSize());
        pageVO.setCurrPage(resultPage.getCurrent());
        pageVO.setTotalPage(resultPage.getPages());
        return pageVO;
    }
}
